using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class StudentLogin : ControllerBase
    {
        private readonly IRepository<StuUsers> _StuUsers;//学生账号表
        private readonly IRepository<Students> _Students;//学生表
        private readonly IFileUploadService _fileUploadService;//获取头像(暂且)



        public StudentLogin(IRepository<StuUsers> StuUsers, IRepository<Students> Students, IFileUploadService fileUploadService)
        {
            _StuUsers = StuUsers;
            _Students = Students;
            _fileUploadService = fileUploadService;
        }
        [HttpPost("/StudentLogin")]
        public async Task<object> login(StudentLoginDto StudentLoginDto)
        {
            var UploadController = new UploadController(_fileUploadService);//用来获取头像
                                                                            //学生登录
                                                                            //头像id在这张表中
            var StuUsers = _StuUsers.Table.ToList();
            //判断学生表中是否有该学生
            var loginUser = StuUsers.Where(x => x.UserId == StudentLoginDto.UserId).ToList();

            if (loginUser.Count == 1)
            {//有该用户
             //先获取所有学生ToList
                var stu = _Students.Table.ToList();


                //需要在Students表上拿取该学生信息
                var student = stu.Where(x => x.StudentId == loginUser[0].UserId).ToList();
                //获取学生头像
                var Avatar = await UploadController.GetFile(loginUser[0].AvatarId);

                //身份为学生所以不需要获取职位


                var msg = new
                {
                    student[0].StudentId,//学号
                    student[0].StudentName,
                    // student[0].Sex,
                    // student[0].IdentityNumber,
                    // student[0].PhoneNumber,
                    // student[0].ChattingNumber,
                    // student[0].QQNumber,
                    // student[0].Address,                       
                    // student[0].FatherName,                       
                    // student[0].FatherPhone,                       
                    // student[0].MontherName,                       
                    // student[0].MontherPhone,                       
                    Avatar.ContentType,
                    Avatar.FileContents,


                };
                //获取token
                // var token = await _IdentityService.ValidateUserAsync(loginDto);
                return new
                {
                    code = 200,
                    msg = msg,
                    data = "登录成功",
                    // token
                };
            }
            else
            {//查无该用户
                return new
                {
                    code = 200,
                    msg = "查无该用户",
                    data = ""
                };

            }
        }

        //小程序中获取学生消息
        [HttpGet("/GetStuInfo/{StudentId}")]
        public async Task<object> GetStuInfo(string StudentId)
        {
            var s = _Students.Table.ToList();
            var StuInfo = s.Where(x => x.StudentId == StudentId).FirstOrDefault();
            if (StuInfo == null)
            {
                return new
                {
                    code = 200,
                    msg = "查无此学生",
                    data = ""
                };
            }
            else
            {
                var obj = new
                {
                    StuInfo.Id,
                    StuInfo.StudentId,
                    StuInfo.StudentName,
                    StuInfo.Sex,
                    //StuInfo.QQNumber,
                    StuInfo.PhoneNumber
                };
                return new
                {
                    code=200,
                    msg="获取成功！！",
                    data=obj
                };

            }
        }

    }
}